package ratelimit.distribute;

import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @Auther: Fazhan.Qiao
 * @Date: 2019/5/28 14:04
 * @Description:
 */
@Configuration
@ConditionalOnExpression("'${sweet.framework.redis.ratelimit:false}'=='true'")
public class RedisLimitWebMvcConfigurer extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(rateLimitCheckInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }


    @Bean
    public HandlerInterceptor rateLimitCheckInterceptor(){
        return  new RateLimitCheckInterceptor();
    }

    @Bean
    public RedisRateLimiterFactory RedisRateLimiterFactory(){
        return  new RedisRateLimiterFactory();
    }
}
